package com.pactera.asmp.server.common.threadpool;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * <p>Class: com.lzq.plugin.threadpool.Task</p>
 * <p>Description: </p>
 * <pre>
 *
 * </pre>
 *
 * @author lv zhiqiang
 * @date 2019/7/17/11:12
 */
public abstract class BaseTask implements IWorkTask, Delayed {
    protected String taskName;
    protected long expire = 0;
    protected long delayTime;

    public BaseTask(String taskName, long delay) {
        this.expire = System.currentTimeMillis() + delay;
        this.taskName = taskName;
        this.delayTime = delay;
    }

    @Override
    public void run() {
        executeWorkTask();
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(this.expire - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        return (int) (this.getDelay(TimeUnit.MILLISECONDS) - o.getDelay(TimeUnit.MILLISECONDS));
    }

    public BaseTask(String taskName) {
        this.taskName = taskName;
    }

    public String getTaskName() {
        return taskName;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    public long getExpire() {
        return expire;
    }

    @Override
    public String toString() {
        return "DelayedTask [ delayTime= " + delayTime +  " , expire=" + expire + ", taskName=" + taskName + "]";
    }
}
